package com.hdcircles.autoform.presenter;

import com.hdcircles.autoform.base.RxPresenter;
import com.hdcircles.autoform.model.bean.FormBean;
import com.hdcircles.autoform.model.http.HttpResponse;
import com.hdcircles.autoform.model.http.RetrofitHelper;
import com.hdcircles.autoform.presenter.contract.FormServiceContract;
import com.hdcircles.autoform.util.LogUtil;
import com.hdcircles.autoform.util.RxUtil;

import java.util.List;

import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by hdcircles on 2016/11/21.
 */

public class FormServicePresenter extends RxPresenter<FormServiceContract.View> implements FormServiceContract.Presenter {


    private final RetrofitHelper retrofitHelper;

    public FormServicePresenter(){
        retrofitHelper = new RetrofitHelper();
    }

    @Override
    public void getFormData(String email) {
        FormBean params=new FormBean();
        params.setReceiver(email);
        Subscription subscription=retrofitHelper.getFormList(params)
                .compose(RxUtil.<HttpResponse<List<FormBean>>>rxScheduleHelper())
                .compose(RxUtil.<List<FormBean>>handleResult())
                .subscribe(new Action1<List<FormBean>>() {
                    @Override
                    public void call(List<FormBean> formBeanList) {
                        mView.sendBroadcast(formBeanList);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtil.d(throwable.getMessage());
                        mView.showError(throwable.getMessage());
                    }
                });
        addSubscribe(subscription);
    }
}
